g_mtProfits={};

local function CompareSumPorfit(a,b)
	return b.sumProfit < a.sumProfit;
end

local function CompareMargin(a,b)
	return b.singleItemProfit/b.cost < a.singleItemProfit/a.cost;
end

local function ComputeProfits()
	g_mtProfits={};
	local scannerDb1=vendor.ScanSnapshot:new(VendorDb.namespaces.Scanner.factionrealm["Alliance - 深渊之巢"].snapshot);
	local scannerDb2=vendor.ScanSnapshot:new(VendorDb.namespaces.Scanner.factionrealm["Horde - 深渊之巢"].snapshot);
	
	local index1=1;
	local index2=1;
	local snap1=scannerDb1:GetScanSetBy1stIndex(index1);
	local snap2=scannerDb2:GetScanSetBy1stIndex(index2);
	
	while ( snap1 and snap2 ) do  
		local key1=snap1:Get(1);
		local key2=snap2:Get(1);
		
		--MyTricksInfo("itemId1="..itemId1.."itemId2="..itemId2);
		if key1 > key2 then
			index2=index2+snap2:Size();
			snap2=scannerDb2:GetScanSetBy1stIndex(index2);
		elseif key2 > key1 then
			index1=index1+snap1:Size();
			snap1=scannerDb1:GetScanSetBy1stIndex(index1);
		elseif snap1:GetItemLink() then		
			local profitItem={};
			local avgMinBid1, avgBuyout1, minMinBid1, minBuyout1, sum1=GetScanSetAvgs(snap1);
			local avgMinBid2, avgBuyout2, minMinBid2, minBuyout2, sum2=GetScanSetAvgs(snap2);
			if avgBuyout1 < avgMinBid2*0.95 then
				profitItem.itemLink=snap1:GetItemLink();
				profitItem.singleItemProfit=avgMinBid2*0.95-avgBuyout1;
				profitItem.cost=avgBuyout1;
				if sum1 < sum2 then
					profitItem.expectedSales = sum1;
				else
					profitItem.expectedSales = sum2;
				end
				profitItem.sumProfit=profitItem.singleItemProfit*profitItem.expectedSales;
				profitItem.iBusinessType = 1;
				
				g_mtProfits[table.getn(g_mtProfits)+1]=profitItem;
			elseif avgBuyout2 < avgMinBid1*0.95 then
				profitItem.itemLink=snap1:GetItemLink();
				profitItem.singleItemProfit=avgMinBid1*0.95-avgBuyout2;
				profitItem.cost=avgBuyout2;
				if sum1 < sum2 then
					profitItem.expectedSales = sum1;
				else
					profitItem.expectedSales = sum2;
				end
				profitItem.sumProfit=profitItem.singleItemProfit*profitItem.expectedSales;
				profitItem.iBusinessType = 2;
				
				g_mtProfits[table.getn(g_mtProfits)+1]=profitItem;
			end
			--for test
			--if table.getn(g_mtProfits) >= 100 then 
			--	break;
			--end
			index1=index1+snap1:Size();
			snap1=scannerDb1:GetScanSetBy1stIndex(index1);
			index2=index2+snap2:Size();
			snap2=scannerDb2:GetScanSetBy1stIndex(index2);
		end	
	end
end

function ShowExpectedProfit(sortType,firstN,bRecompute,bAllFiction)
	local factionKey = UnitFactionGroup("player");
	local factionBusinessType;
	
	if factionKey == "Alliance" or factionKey == "联盟" then 
		factionBusinessType = 1 ;
	else
		factionBusinessType = 2 ;
	end
	
	if bRecompute or table.getn(g_mtProfits) == 0 then
		ComputeProfits();
	end
	if sortType == 1 then
		table.sort(g_mtProfits, CompareSumPorfit);
	elseif sortType == 2 then
		table.sort(g_mtProfits, CompareMargin);	
	end
	
	MyTricksInfo("ExpectedProfit made by caster");
	MyTricksInfo("Item  expectedSales  SumProfit(Gold)  Margin(Gold)");
	local count=0;
	for i = 1,table.getn(g_mtProfits) do
		--MyTricksInfo("i="..i.."type(g_mtProfits[i].itemLink)="..type(g_mtProfits[i].itemLink));
		--MyTricksInfo("i="..i.."type(g_mtProfits[i].sumProfit)="..type(g_mtProfits[i].sumProfit));
		if not g_mtProfits[i].itemLink then
			g_mtProfits[i].itemLink=""
		end
		if g_mtProfits[i].expectedSales >= 5 and g_mtProfits[i].sumProfit >= 80*10000 and ( bAllFiction or g_mtProfits[i].iBusinessType == factionBusinessType ) then
			MyTricksInfo(g_mtProfits[i].itemLink.."  "..g_mtProfits[i].expectedSales.."  "..(g_mtProfits[i].sumProfit/10000).." "..(g_mtProfits[i].singleItemProfit/g_mtProfits[i].cost));
			count=count+1;
			if count >= firstN then
				break;
			end
		end
	end
	
end

function BindExpectedProfitCompute()
	local index=GetMacroIndexByName("ShowExpectedProfit");
	if index == 0 then
		local sMacroBody="/script ShowExpectedProfit(1,15,false) \n";
		CreateMacro("ShowExpectedProfit", 101, sMacroBody, nil,nil);
	end
	
	local index=GetMacroIndexByName("ShowMargin");
	if index == 0 then
		local sMacroBody="/script ShowExpectedProfit(2,20,false) \n";
		CreateMacro("ShowMargin", 101, sMacroBody, nil,nil);
	end
	
	index=GetMacroIndexByName("RecomputeExpectedProfit");
	if index == 0 then
		local sMacroBody="/script ShowExpectedProfit(1,15,true) \n";
		CreateMacro("RecomputeExpectedProfit", 102, sMacroBody, nil,nil);
	end
end